package cn.com.cetccst.kvmanager;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.tencent.mmkv.MMKV;

public final class KVStoreImp implements IKVStore {
    private static MMKV mmkv;

    public KVStoreImp(Context context, String fileName, int model, String cryptKey) {
        String defaultRootPath = MMKV.initialize(context);
        if (TextUtils.isEmpty(fileName)) {
            fileName = defaultRootPath;
        }
        if (TextUtils.isEmpty(cryptKey)) {
            mmkv = MMKV.mmkvWithID(fileName, model);
        } else {
            mmkv = MMKV.mmkvWithID(fileName, model, cryptKey);
        }
    }

    public void encode(String key, String value) {
        mmkv.encode(key, value);
    }

    @Override
    public void encode(String key, int value) {
        mmkv.encode(key, value);
    }

    @Override
    public void encode(String key, long value) {
        mmkv.encode(key, value);
    }

    @Override
    public void encode(String key, boolean value) {
        mmkv.encode(key, value);
    }

    public String decodeString(String key) {
        return mmkv.decodeString(key);
    }

    @Override
    public int decodeInt(String key) {
        return mmkv.decodeInt(key);
    }

    @Override
    public long decodeLong(String key) {
        return mmkv.decodeLong(key);
    }

    @Override
    public boolean decodeBool(String key) {
        return mmkv.decodeBool(key);
    }

    @Override
    public String decodeString(String key, @NonNull String defaultValue) {
        return mmkv.decodeString(key, defaultValue);
    }

    @Override
    public int decodeInt(String key, int defaultValue) {
        return mmkv.decodeInt(key, defaultValue);
    }

    @Override
    public long decodeLong(String key, long defaultValue) {
        return mmkv.decodeLong(key, defaultValue);
    }

    @Override
    public boolean decodeBool(String key, boolean defaultValue) {
        return mmkv.decodeBool(key, defaultValue);
    }

    public void clear(String key) {
        mmkv.remove(key).apply();
    }

    @Override
    public void clear() {
        mmkv.clearAll();
    }

    public static String[] keys() {
        return mmkv.allKeys();
    }

}
